<%@ page import="com.gxwgy.wg2404.pojo.EventsPojo" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>活动列表</title>
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui.css">
  <style>
    body {
      padding: 20px;
      background-color: #f2f2f2;
    }
    .table-container {
      margin: 20px auto;
      background-color: #fff;
      padding: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }
  </style>
</head>
<body>
<div class="layui-container">
  <h1 class="layui-text">活动列表</h1>
  <div class="table-container">
    <table class="layui-table">
      <thead>
      <tr>
        <th>活动名称</th>
        <th>开始时间</th>
        <th>结束时间</th>
        <th>地点</th>
        <th>当前报名人数</th>
        <th>剩余名额</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <%
        // 获取传递过来的活动列表
        List<EventsPojo> events = (List<EventsPojo>) request.getAttribute("events");
        if (events != null && !events.isEmpty()) {
          for (EventsPojo event : events) {
      %>
      <tr data-event-id="<%= event.getId() %>">
        <td><%= event.getName() %></td>
        <td><%= event.getStart_time() %></td>
        <td><%= event.getEnd_time() %></td>
        <td><%= event.getLocation() %></td>
        <td class="current-enrollment"><%= event.getCurrent_enrollment() %></td> <!-- 显示当前报名人数 -->
        <td class="remaining-spots"><%= event.getCapacity() - event.getCurrent_enrollment() %></td>
        <td>
          <% if (event.getCapacity() - event.getCurrent_enrollment() > 0) { %>
          <form action="<%=request.getContextPath()%>/Enrollment" method="post" style="display: inline;">
            <input type="hidden" name="eventId" value="<%= event.getId() %>">
            <button type="button" class="layui-btn layui-btn-sm enroll-btn">报名</button>
          </form>
          <% } else { %>
          <button class="layui-btn layui-btn-disabled layui-btn-sm">已满员</button>
          <% } %>
        </td>
      </tr>
      <%
        }
      } else {
      %>
      <tr>
        <td colspan="7" class="layui-text-center">暂无活动</td>
      </tr>
      <% } %>
      </tbody>
    </table>
  </div>
</div>

<script src="${pageContext.request.contextPath}/static/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    $(".enroll-btn").click(function(event) {
      event.preventDefault();  // 阻止默认的表单提交行为

      var form = $(this).closest("form");  // 获取当前点击按钮的表单
      var eventId = form.find("input[name='eventId']").val();  // 获取活动 ID

      $.ajax({
        type: "POST",
        url: "<%=request.getContextPath()%>/Enrollment",
        data: { eventId: eventId },
        success: function(response) {
          if (response === "success") {
            alert("报名成功！");
            // 页面刷新
            window.location.reload();  // 刷新当前页面
          } else {
            alert(response);  // 错误信息
          }
        },
        error: function() {
          alert("报名失败，请稍后再试！");
        }
      });

    });
  });

  // 更新活动信息函数
  function updateEventInfo(eventId) {
    var row = $("tr[data-event-id='" + eventId + "']");
    var remainingSpots = row.find(".remaining-spots");
    var currentEnrollment = row.find(".current-enrollment");

    $.ajax({
      type: "GET",
      url: "<%=request.getContextPath()%>/GetEventInfo",  // 获取活动信息的 Servlet
      data: { eventId: eventId },
      success: function(response) {
        console.log(response);  // 打印返回的数据，检查是否正常

        // 确保返回数据的格式正确
        if (response.currentEnrollment !== undefined && response.remainingSpots !== undefined) {
          remainingSpots.text(response.remainingSpots);  // 更新剩余名额
          currentEnrollment.text(response.currentEnrollment);  // 更新当前报名人数
        } else {
          console.error("返回数据格式错误！");
        }
      },
      error: function() {
        alert("更新活动信息失败！");
      }
    });
  }

</script>

</body>
</html>
